<template>
  <div class="login-page">
    <!-- 添加粒子背景 -->
    <div class="particles-container">
      <div v-for="n in 20" :key="n" class="particle" :style="{ 
        '--delay': `${Math.random() * 5}s`,
        '--duration': `${Math.random() * 10 + 10}s`,
        '--position': `${Math.random() * 100}%`,
        '--size': `${Math.random() * 30 + 10}px`
      }"></div>
    </div>
    
    <div class="login-container">
      <div class="login-card">
        <div class="glow-effect"></div>
        
        <div class="login-header">
          <div class="icon-container">
            <i class="el-icon-lock floating-icon"></i>
          </div>
          <h2 class="magical-title">学信链</h2>
          <div class="subtitle">基于区块链的教育证书管理系统</div>
          <div class="magical-underline"></div>
        </div>
        
        <div class="login-form">
          <el-form 
            ref="loginForm_ref" 
            :model="loginForm" 
            :rules="loginRules" 
            label-position="top"
          >
            <el-form-item label="以太坊地址" prop="address">
              <el-input 
                v-model="loginForm.address" 
                prefix-icon="el-icon-key" 
                placeholder="请输入智能合约管理员地址"
              >
                <template #suffix v-if="loginForm.address">
                  <div class="magic-effect-container">
                    <div class="magic-sparkle"></div>
                  </div>
                </template>
              </el-input>
              <div class="form-tip">以0x开头的以太坊地址，必须是合约部署者</div>
            </el-form-item>
            
            <el-form-item>
              <div class="remember-me">
                <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              </div>
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                class="login-button magical-btn" 
                :loading="loading" 
                @click="handleLogin"
              >
                <div class="btn-bg-effect"></div>
                验证管理员身份
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        
        <!-- 添加帮助信息 -->
        <div class="login-help">
          <div class="help-icon">
            <i class="el-icon-question"></i>
          </div>
          <div class="help-text">
            <p>本系统仅限智能合约管理员使用</p>
            <p>管理员为部署合约时的账户地址</p>
          </div>
        </div>
        
        <div class="login-footer">
          <p>© {{ new Date().getFullYear() }} 学信链证书管理系统</p>
          <p class="footer-links">
            <a href="#">隐私政策</a> | <a href="#">服务条款</a> | <a href="#">关于系统</a>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'

export default {
  name: 'LoginPage',
  setup() {
    const router = useRouter()
    const store = useStore()
    const loginForm = reactive({
      address: ''
    })
    
    const loginRules = {
      address: [
        { required: true, message: '请输入以太坊地址', trigger: 'blur' },
        { pattern: /^0x[a-fA-F0-9]{40}$/, message: '请输入有效的以太坊地址', trigger: 'blur' }
      ]
    }
    
    const loading = ref(false)
    const rememberMe = ref(false)
    const loginForm_ref = ref(null)
    
    // 检查本地存储的地址
    const checkStoredAddress = () => {
      const storedAddress = localStorage.getItem('adminAddress')
      if (storedAddress) {
        loginForm.address = storedAddress
      }
    }
    
    // 页面加载时检查本地存储
    checkStoredAddress()
    
    const handleLogin = () => {
      loginForm_ref.value.validate(async (valid) => {
        if (valid) {
          try {
            loading.value = true
            
            // 调用store中的verifyAdmin方法来验证地址
            const result = await store.dispatch('verifyAdmin', loginForm.address)
            
            if (result && result.verified) {
              // 保存登录状态
              localStorage.setItem('isLoggedIn', 'true')
              localStorage.setItem('adminAddress', loginForm.address)
              
              if (rememberMe.value) {
                localStorage.setItem('username', '管理员')
              }
              
              ElMessage.success('管理员身份验证成功')
              router.push('/students')
            } else {
              ElMessage.error(result?.message || '验证失败，请确认您是合约管理员')
            }
          } catch (error) {
            console.error('登录失败:', error)
            ElMessage.error('登录失败: ' + error.message)
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    return {
      loginForm,
      loginRules,
      loading,
      rememberMe,
      loginForm_ref,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-page {
  height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
  overflow: hidden;
}

/* 粒子背景效果 */
.particles-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  overflow: hidden;
  pointer-events: none;
}

.particle {
  position: absolute;
  width: var(--size);
  height: var(--size);
  background: radial-gradient(circle, rgba(173,216,230,0.8) 0%, rgba(173,216,230,0) 70%);
  border-radius: 50%;
  top: calc(var(--position) - 50px);
  left: calc(var(--position) - 50px);
  animation: float var(--duration) ease-in-out var(--delay) infinite;
  opacity: 0.4;
  filter: blur(5px);
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) translateX(0) scale(1);
    opacity: 0.2;
  }
  25% {
    transform: translateY(-100px) translateX(50px) scale(1.2);
    opacity: 0.4;
  }
  50% {
    transform: translateY(-30px) translateX(100px) scale(0.8);
    opacity: 0.6;
  }
  75% {
    transform: translateY(-70px) translateX(30px) scale(1.1);
    opacity: 0.4;
  }
}

.login-container {
  width: 100%;
  max-width: 450px;
  padding: 0 20px;
  z-index: 1;
}

.login-card {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  padding: 40px;
  position: relative;
  transition: all 0.5s;
}

.login-card:hover {
  box-shadow: 0 20px 40px rgba(52, 179, 241, 0.2);
  transform: translateY(-5px);
}

.glow-effect {
  position: absolute;
  top: -100px;
  left: -100px;
  width: 200px;
  height: 200px;
  background: radial-gradient(circle, rgba(97,73,205,0.3) 0%, rgba(97,73,205,0) 70%);
  border-radius: 50%;
  filter: blur(20px);
  animation: moveGlow 10s infinite alternate ease-in-out;
  z-index: 0;
  pointer-events: none;
}

@keyframes moveGlow {
  0% {
    top: -100px;
    left: -100px;
  }
  25% {
    top: 30%;
    left: 80%;
  }
  50% {
    top: 70%;
    left: 20%;
  }
  75% {
    top: 10%;
    left: 50%;
  }
  100% {
    top: 50%;
    left: 80%;
  }
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
  position: relative;
}

.icon-container {
  width: 60px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #34b3f1, #6149cd);
  border-radius: 50%;
  margin: 0 auto 15px;
  position: relative;
  overflow: hidden;
}

.icon-container::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(45deg, transparent, rgba(255,255,255,0.8), transparent);
  transform: rotate(45deg);
  animation: shimmer 3s infinite;
  pointer-events: none;
}

@keyframes shimmer {
  0% { transform: translateX(-100%) rotate(45deg); }
  100% { transform: translateX(100%) rotate(45deg); }
}

.floating-icon {
  animation: floatingIcon 3s ease-in-out infinite;
  color: white;
  font-size: 30px;
}

@keyframes floatingIcon {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

.magical-title {
  font-size: 24px;
  font-weight: 700;
  margin: 10px 0 5px;
  color: transparent;
  background: linear-gradient(45deg, #34b3f1, #6149cd, #6149cd, #34b3f1);
  background-size: 300% 300%;
  background-clip: text;
  -webkit-background-clip: text;
  animation: gradientText 6s ease infinite;
  position: relative;
  display: inline-block;
  letter-spacing: 1px;
}

.subtitle {
  font-size: 14px;
  color: #606266;
  margin-bottom: 15px;
}

@keyframes gradientText {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.magical-underline {
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 100px;
  height: 2px;
  background: linear-gradient(90deg, rgba(97,73,205,0) 0%, rgba(97,73,205,1) 50%, rgba(97,73,205,0) 100%);
}

.login-form {
  margin-top: 20px;
  position: relative;
  z-index: 1;
}

:deep(.el-form-item__label) {
  color: #4a5568;
  font-weight: 500;
  font-size: 15px;
}

:deep(.el-input__inner) {
  height: 45px;
  border-radius: 8px;
  border: 1px solid #e0e5ec;
  transition: all 0.3s;
}

:deep(.el-input__inner:focus) {
  border-color: #6149cd;
  box-shadow: 0 0 0 2px rgba(97, 73, 205, 0.2);
}

.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
  line-height: 1.4;
}

.remember-me {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

:deep(.el-checkbox__label) {
  color: #606266;
}

.login-button {
  width: 100%;
  height: 45px;
  border-radius: 25px;
  font-size: 16px;
  background: linear-gradient(to right, #34b3f1, #6149cd);
  border: none;
  font-weight: 500;
  letter-spacing: 1px;
  position: relative;
  overflow: hidden;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 7px 14px rgba(97, 73, 205, 0.2);
  background: linear-gradient(to right, #6149cd, #34b3f1);
}

.btn-bg-effect {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(45deg, #34b3f1, #6149cd, #6149cd, #34b3f1);
  background-size: 300% 300%;
  animation: gradientBg 3s ease infinite;
  opacity: 0;
  transition: opacity 0.3s;
  z-index: -1;
}

.magical-btn:hover .btn-bg-effect {
  opacity: 0.2;
}

@keyframes gradientBg {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* 表单特效 */
.magic-effect-container {
  position: relative;
  width: 20px;
  height: 20px;
}

.magic-sparkle {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(circle, #6149cd 0%, transparent 60%);
  border-radius: 50%;
  animation: sparkle 2s infinite;
}

@keyframes sparkle {
  0%, 100% { transform: scale(0.5); opacity: 0.2; }
  50% { transform: scale(1.2); opacity: 0.8; }
}

/* 帮助信息样式 */
.login-help {
  display: flex;
  align-items: center;
  margin: 20px 0;
  padding: 15px;
  background-color: rgba(52, 179, 241, 0.1);
  border-radius: 8px;
  border-left: 3px solid #34b3f1;
}

.help-icon {
  margin-right: 15px;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #34b3f1;
  border-radius: 50%;
  color: white;
  flex-shrink: 0;
}

.help-text {
  font-size: 13px;
  color: #606266;
  line-height: 1.5;
}

.help-text p {
  margin: 0;
  padding: 2px 0;
}

.login-footer {
  text-align: center;
  margin-top: 20px;
}

.copyright {
  font-size: 12px;
  color: #909399;
  margin-top: 10px;
}

/* 适应小屏幕设备 */
@media screen and (max-width: 576px) {
  .login-card {
    padding: 25px;
  }
  
  .login-header {
    margin-bottom: 20px;
  }
  
  .magical-title {
    font-size: 20px;
  }
  
  .icon-container {
    width: 50px;
    height: 50px;
  }
  
  .floating-icon {
    font-size: 24px;
  }
}
</style> 